package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.limao.DBDataClass.SysCidDataClass;
import com.limao.DbConnection.DbConnectionManager;

public class SysCidProvider  implements java.io.Serializable{
	

	private static final long serialVersionUID = -4256558234633788680L;


	private static class SysCidProviderContainer {
		private static SysCidProvider instance = new SysCidProvider();
	}

	public static SysCidProvider getInstance() {
		return SysCidProviderContainer.instance;
	}
	
	

	
	public SysCidDataClass loadUserCidByCid(String cid) {
		SysCidDataClass userApp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select * from cd_sys_cid where cid=? ");
			pstmt.setString(1, cid);
			rs = pstmt.executeQuery();
			if(rs.next()){
				userApp = new SysCidDataClass();
				userApp.userid = rs.getInt("userid")+"";
				userApp.cid = rs.getString("cid");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		
		return userApp;
	}
	
	
	//新增注册用户App使用信息
	public SysCidDataClass insesrtUserCid(String cid,int userid) {
		SysCidDataClass userApp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("replace into cd_sys_cid(cid, userid) values(?,?)");
			pstmt.setString(1, cid);
			pstmt.setInt(2, userid);
			pstmt.execute();
			
			userApp = new SysCidDataClass();
			userApp.cid = cid;
			userApp.userid = userid + "";
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(null, pstmt, con);
		}
		
		return userApp;
	}
		
	//修改注册用户App使用信息
	public SysCidDataClass updateUserCid(String cid, int userid) {
		SysCidDataClass userApp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("update cd_sys_cid set userid=? where cid=?");
			pstmt.setInt(1, userid);
			pstmt.setString(2, cid);
			pstmt.executeUpdate();
			
			userApp = new SysCidDataClass();
			userApp.userid = userid + "";
			userApp.cid = cid;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(null, pstmt, con);
		}
		
		return userApp;
	}
		
	
	//根据用户id取cid
	public ArrayList<SysCidDataClass> loadUserCid(int userid) {
		
		ArrayList<SysCidDataClass> list = new ArrayList<SysCidDataClass>();;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select * from cd_sys_cid where userid=? ");
			pstmt.setInt(1, userid);
			rs = pstmt.executeQuery();
			while(rs.next()){
				SysCidDataClass obj=new SysCidDataClass();
				obj.cid=rs.getString("cid");
				obj.userid=userid+"";
				list.add(obj);
			}
		} catch (Exception e) {
			list.clear();
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		
		return list;
	}
	
}
